/**
 * Copyright (C) BFH www.bfh.ch 2011
 * Code written by: Patrick Dobler, Marc Folly
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.ti.kybernetik.gui.swing;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import net.miginfocom.swing.MigLayout;
import ch.bfh.ti.kybernetik.engine.controller.Simulator;
import ch.bfh.ti.kybernetik.engine.controller.lightBulb.LightBulbController;
import ch.bfh.ti.kybernetik.engine.controller.lightBulb.LightBulbControllerFactory;
import ch.bfh.ti.kybernetik.engine.controller.roboter.RoboterController;
import ch.bfh.ti.kybernetik.engine.controller.roboter.RoboterControllerFactory;
import ch.bfh.ti.kybernetik.gui.slick.GuiSimulator;
import ch.bfh.ti.kybernetik.gui.slick.GuiSimulatorListener;
import ch.bfh.ti.kybernetik.gui.slick.components.LightBulbComponent;
import ch.bfh.ti.kybernetik.gui.slick.components.RoboterComponent;

/**
 * A Swing based control GUI to add further control functions to the
 * {@link GuiSimulator}.
 * 
 * Due the fact that the Slick based {@link GuiSimulator} is limited in
 * controlling functions, such as adding new roboters and control their
 * behaviors, we added a second GUI to handle these operations
 * 
 */
public class SimulatorControlGuiFrame extends JFrame implements GuiSimulatorListener {

	private static final long serialVersionUID = 1L;

	private GuiSimulator guiSimulator;

	private Simulator simulator;

	private RoboterComponent selectedRoboterComponent;

	private JButton editRoboterButton;

	private EditRoboterFrame editRoboterFrame;

	private EditLightBulbFrame editLightBulbFrame;

	private LightBulbComponent selectedLightBulbComponent;

	private JButton editLightBulbButton;

	private JCheckBox renderTracingCheckBox;

	public SimulatorControlGuiFrame(Simulator simulator, GuiSimulator guiSimulator) {
		this.simulator = simulator;
		setTitle("Simulator Controller");
		setSize(350, 280);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.guiSimulator = guiSimulator;
		this.initGUI();
		guiSimulator.registerGuiSimulatorListener(this);
	}

	public final void initGUI() {
		JPanel panel = new JPanel(new MigLayout());
		getContentPane().add(panel);

		panel.add(new JLabel("Add Roboter"));
		panel.add(getAddRandomRoboterButton(), "wrap");

		panel.add(new JLabel("Add LightBulb"));
		panel.add(getAddRandomLightBulbButton(), "wrap");

		panel.add(new JLabel("Edit Selected LightBulb"));
		panel.add(getEditSelectedLightBulbButton(), "wrap");

		panel.add(new JLabel("Edit Selected Roboter"));
		panel.add(getEditSelectedRoboterButton(), "wrap");

		panel.add(new JLabel("Render Tracing"));
		panel.add(getRenderTracingCheckBox(), "wrap");

		panel.add(new JLabel("Simulator Speed"));
		panel.add(getSimulatorSpeedInputComponent(), "wrap");

		panel.add(new JLabel("Simulator Delta T"));
		panel.add(getDeltaTInputComponent(), "wrap");

	}

	private JButton getEditSelectedRoboterButton() {
		if (editRoboterButton == null) {
			editRoboterButton = new JButton("Edit Selected Roboter");
			editRoboterButton.setEnabled(false);
			editRoboterButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					editRoboterFrame = new EditRoboterFrame(selectedRoboterComponent);
					editRoboterFrame.setVisible(true);
				}
			});
		}
		return editRoboterButton;
	}

	private JButton getEditSelectedLightBulbButton() {
		if (editLightBulbButton == null) {
			editLightBulbButton = new JButton("Edit Selected LightBulb");
			editLightBulbButton.setEnabled(false);
			editLightBulbButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					editLightBulbFrame = new EditLightBulbFrame(selectedLightBulbComponent);
					editLightBulbFrame.setVisible(true);
				}
			});
		}
		return editLightBulbButton;
	}

	private JButton getAddRandomLightBulbButton() {
		JButton addRoboterButton = new JButton("Create Random LightBulb");
		addRoboterButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				LightBulbController lightBulbController = LightBulbControllerFactory.createRandomLightBulbConroller(simulator.getWidth(),
						simulator.getWidth());

				simulator.addLightBulbController(lightBulbController);
			}
		});
		return addRoboterButton;
	}

	private JButton getAddRandomRoboterButton() {
		JButton addRoboterButton = new JButton("Create Random Roboter");
		addRoboterButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				RoboterController aRoboterController = RoboterControllerFactory.createRandomRoboterController(simulator.getWidth(),
						simulator.getHeight());
				simulator.addRoboterController(aRoboterController);

			}
		});
		return addRoboterButton;
	}

	private JComponent getSimulatorSpeedInputComponent() {
		JSlider simulatorSpeedSlider = new JSlider(JSlider.HORIZONTAL, 1, 500, simulator.getSimulatorSpeed());
		simulatorSpeedSlider.setPaintLabels(true);
		simulatorSpeedSlider.setMajorTickSpacing(25);
		simulatorSpeedSlider.setPaintTicks(true);
		Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
		labelTable.put(new Integer(1), new JLabel("Fast"));
		labelTable.put(new Integer(500), new JLabel("Slow"));
		simulatorSpeedSlider.setLabelTable(labelTable);
		simulatorSpeedSlider.setPaintLabels(true);
		simulatorSpeedSlider.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				JSlider source = (JSlider) e.getSource();
				simulator.setSimulatorSpeed(source.getValue());
			}
		});
		return simulatorSpeedSlider;
	}

	private JCheckBox getRenderTracingCheckBox() {
		if (renderTracingCheckBox == null) {
			renderTracingCheckBox = new JCheckBox();
			renderTracingCheckBox.setSelected(guiSimulator.isRenderTracing());
			renderTracingCheckBox.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					guiSimulator.setRenderTracing(renderTracingCheckBox.isSelected());
				}
			});
		}

		return renderTracingCheckBox;

	}

	private JComponent getDeltaTInputComponent() {
		final JTextField textField = new JTextField(100);
		textField.setText(String.valueOf(simulator.getDeltaT()));
		textField.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String newText = textField.getText();
				try {
					Double newValue = Double.valueOf(newText);
					simulator.setDeltaT(newValue);
				} catch (NumberFormatException nfe) {
					System.out.println("Invalid Input for Delta T: " + nfe);
				}
			}
		});
		return textField;

	}

	@Override
	public void selctedRoboterComponent(RoboterComponent newSelectedRc) {
		if (newSelectedRc != null) {
			this.selectedRoboterComponent = newSelectedRc;
			this.hideAllEditFrames();
			this.getEditSelectedRoboterButton().setEnabled(true);
		}
	}

	@Override
	public void selctedLightComponent(LightBulbComponent newSelectedLightBulbComponent) {
		if (newSelectedLightBulbComponent != null) {
			this.selectedLightBulbComponent = newSelectedLightBulbComponent;
			this.hideAllEditFrames();
			this.getEditSelectedLightBulbButton().setEnabled(true);
		}
	}

	private void hideAllEditFrames() {
		if (editRoboterFrame != null) {
			editRoboterFrame.setVisible(false);
		}
		if (editLightBulbFrame != null) {
			editLightBulbFrame.setVisible(false);
		}
		this.getEditSelectedRoboterButton().setEnabled(false);
		this.getEditSelectedLightBulbButton().setEnabled(false);
	}
}
